package cn.touna.jss.common.factory;

import cn.touna.risk.threadpool.common.DefaultThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 线程池配置
 * Created by Steve.Tao on 2017/9/8.
 */
@Configuration
public class ThreadPoolConfiguration {

    @Bean("scheduledThredPool")
    public ScheduledExecutorService scheduledThredPool() {
        return Executors.newScheduledThreadPool(1, new DefaultThreadFactory("jss-scheduledThredPool"));
    }

    /**
     * 线程池<br/>
     * 1. 用于第三方接口 限时连接
     * @return
     */
    @Bean("executorService")
    public ExecutorService executorService(){
        ThreadFactory threadFactory = new DefaultThreadFactory("jss-ThredPool");
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() , 200,
            60L, TimeUnit.SECONDS,
            new LinkedBlockingDeque<Runnable>(),
            threadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        return threadPoolExecutor;
    }
}
